package com.lancoo.answer.util.paperUtils;

import android.util.Log;

import androidx.fragment.app.Fragment;

import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.TaskState;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.entity.TaskOperateBean;
import com.lancoo.answer.ui.ques.IntroductionFragment;
import com.lancoo.answer.ui.ques.WaitToDockFragment;
import com.lancoo.answer.ui.ques.quesBody.CompoundMatchQuseBodyFragment;
import com.lancoo.answer.ui.ques.quesBody.CompoundMatchQuseBodyLookingFragment;
import com.lancoo.answer.ui.ques.quesBody.CompoundWritingQuesBodyFragment;
import com.lancoo.answer.ui.ques.quesBody.EmptyQuseBodyFragment;
import com.lancoo.answer.ui.ques.quesBody.HightLightQuseBodyFragment;
import com.lancoo.answer.ui.ques.quesBody.ListenArticleQuseBodyFragment;
import com.lancoo.answer.ui.ques.quesBody.NormalQuseBodyFragment;
import com.lancoo.answer.ui.ques.quesBody.TranslationQuseBodyFragment;
import com.lancoo.answer.ui.ques.quesBody.WanXingFillQuseBodyFragment;
import com.lancoo.answer.util.paperUtils.fragmentUtils.child.ChildLookingFragmentUtils;
import com.lancoo.answer.util.paperUtils.fragmentUtils.child.ChildMuteValueteFragmentUtils;
import com.lancoo.answer.util.paperUtils.fragmentUtils.child.ChildOriginalFragmentUtils;
import com.lancoo.answer.util.paperUtils.fragmentUtils.child.ChildTrainingingFragmentUtils;
import com.lancoo.answer.util.paperUtils.fragmentUtils.ques.QuesLookingingFragmentUtils;
import com.lancoo.answer.util.paperUtils.fragmentUtils.ques.QuesMuteValueteFragmentUtils;
import com.lancoo.answer.util.paperUtils.fragmentUtils.ques.QuesOriginalFragmentUtils;
import com.lancoo.answer.util.paperUtils.fragmentUtils.ques.QuesTrainingingFragmentUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengmi on 2021/12/15 19:52
 * Description:
 */
public class FragmentLoadUtils {

    private static final String TAG = "FragmentLoadUtils";


    /**
     * 加载大题Fragment
     *
     * @param taskOperateBean 操作对象
     * @return Fragment
     */
    public static Fragment loadQueseFragment(TaskOperateBean taskOperateBean) {
        int typeIndex = taskOperateBean.getTypeIndex();
        int quesIndex = taskOperateBean.getQuesIndex();
        int defaultLoadIndex = taskOperateBean.getDefaultLoadIndex();
        int defaultLoadChildIndex = taskOperateBean.getDefaultLoadChildIndex();
        int defaultLoadItemIndex = taskOperateBean.getDefaultLoadItemIndex();
        Log.e(TAG, "0726: typeInedx:" +
                taskOperateBean.getTypeIndex() + ",quesIndex:" +
                taskOperateBean.getQuesIndex() + "defaultLoadIndex:" + defaultLoadIndex);
        taskOperateBean.setDefaultLoadIndex(-1);
        taskOperateBean.setDefaultLoadChildIndex(-1);
        taskOperateBean.setDefaultLoadItemIndex(-1);
        if (taskOperateBean.getKindType() == 0) {//加载导语界面
            return IntroductionFragment.loadTopicFragment(taskOperateBean);
        } else {//加载试题界面
//            if (ConstantBean.Companion.getTaskControlBean().isManu()){//人工组卷直接加载小题fragment
//                int childIndex = taskOperateBean.getChildIndex();
//                Ques quesBean = taskOperateBean.getQuesBean();
//                return loadChildFragment(quesBean,typeIndex,quesIndex,childIndex,defaultLoadIndex);
//            }
            //普通试卷加载大题
            Type type = new Type();
            List<Ques> list = new ArrayList<>();
            Ques ques = taskOperateBean.getQuesBean();
            taskOperateBean.setDefaultLoadIndex(-1);
            list.add(ques);
            type.setQuesList(list);
            switch (ConstantBean.getTaskState()) {
                case TRAINING:
                    return QuesTrainingingFragmentUtils.loadQuesTrainingFragment(ques, typeIndex,
                            quesIndex, defaultLoadIndex, defaultLoadChildIndex, defaultLoadItemIndex);
                case LOOKING:
                    return QuesLookingingFragmentUtils.loadQuesLookingFragment(ques, typeIndex, quesIndex, defaultLoadChildIndex, defaultLoadItemIndex, defaultLoadIndex);
                case MUTEVALUATE:
                    return QuesMuteValueteFragmentUtils.loadQuesMuteValueteFragment(ques, typeIndex, quesIndex,defaultLoadChildIndex,defaultLoadItemIndex);
                case ORIGINAL:
                    return QuesOriginalFragmentUtils.loadQuesQriginalFragment(ques, typeIndex, quesIndex, defaultLoadChildIndex, defaultLoadItemIndex, defaultLoadIndex);
                default:
                    return new WaitToDockFragment();
            }
        }

    }

    /**
     * 加载小题fragment
     *
     * @param ques       大题数据模型
     * @param typeIndex  题型Index
     * @param quesIndex  大题Index
     * @param childIndex 小题Index
     * @return Fragment
     */
    public static Fragment loadChildFragment(Ques ques, int typeIndex, int quesIndex, int childIndex, int defaultLoadIndex,int defaultLoadChildexIndex,int defaultLoadItemIndex) {
        String genreID = ques.getGenreID();
        String typeNo = ques.getTypeNo();
        Log.e(TAG, "loadChildFragment: genreID:" + genreID + ",typeNo:" + typeNo);
        TaskState taskState = ConstantBean.getTaskState();
        switch (taskState) {
            case TRAINING://作答
                return ChildTrainingingFragmentUtils.loadChildTrainingFragment(ques, typeIndex, quesIndex, childIndex, defaultLoadIndex,
                        defaultLoadChildexIndex,defaultLoadItemIndex);
            case LOOKING://查看评阅
                return ChildLookingFragmentUtils.loadChildLookingFragment(ques, typeIndex, quesIndex, childIndex, defaultLoadIndex,
                        defaultLoadChildexIndex,defaultLoadItemIndex);
            case MUTEVALUATE://互评状态
                return ChildMuteValueteFragmentUtils.loadChildmuteValueteFragment(ques, typeIndex, quesIndex, childIndex, defaultLoadIndex);
            case ORIGINAL:
                return ChildOriginalFragmentUtils.loadChildOriginalFragment(ques, typeIndex, quesIndex, childIndex, defaultLoadIndex ,defaultLoadChildexIndex,defaultLoadItemIndex);
            default:
                return new WaitToDockFragment();
        }

    }

    /**
     * 加载大题题干fragment
     *
     * @param ques      大题数据模型
     * @param typeIndex 题型Index
     * @param quesIndex 大题Index
     * @return Fragment
     */
    public static Fragment loadQuesBodyFragment(Ques ques, int typeIndex, int quesIndex,
                                                int childIntex, int defaultLoadChildIndex,int defaultItemIndex) {
//        if(true){
//            return EmptyQuseBodyFragment.loadQueseFragment(typeIndex, quesIndex);
//        }
        String typeNo = ques.getTypeNo();
        String genreID = ques.getGenreID();
        TaskState taskState = ConstantBean.Companion.getTaskState();
        switch (genreID) {
            case "39":
            case "41":
            case "50":
            case "51":
                return TranslationQuseBodyFragment.loadQueseFragment(typeIndex, quesIndex, defaultLoadChildIndex);
            case "78": //复合写作
                return CompoundWritingQuesBodyFragment.loadQueseFragment(typeIndex, quesIndex, childIntex, defaultLoadChildIndex);
        }
        switch (typeNo) {
            case "f"://作文题
            case "g"://翻译题
            case "X"://阅读简答
            case "H"://复合题（复合阅读，复合写作，复合改写）
            case "D"://阅读理解（理解、判断）
            case "N"://选词填空
//            case "C"://完型填空
            case "P"://阅读匹配、段落排序
            case "Q"://阅读填空
                if (ConstantBean.Companion.getTaskState() == TaskState.LOOKING
                        && ConstantBean.Companion.getTaskControlBean().getIsOpenLighTabToBody() == 1) {
                    return HightLightQuseBodyFragment.loadQueseFragment(typeIndex, quesIndex, childIntex, defaultLoadChildIndex);
                } else {
                    return NormalQuseBodyFragment.loadQueseFragment(typeIndex, quesIndex, childIntex, defaultLoadChildIndex);
                }
            case "C"://完型填空 暂用
                Log.e("完型填空",childIntex+","+defaultLoadChildIndex+","+defaultItemIndex);
                return WanXingFillQuseBodyFragment.loadQueseFragment(typeIndex, quesIndex, childIntex, defaultLoadChildIndex);
            case "W"://听写
            case "E"://听力选择题
            case "S"://听力填空
            case "G"://听力简答
            case "L"://听力综合
            case "x"://听力匹配
                if (taskState == TaskState.LOOKING || taskState == TaskState.ORIGINAL) {
                    return ListenArticleQuseBodyFragment.loadQueseFragment(typeIndex, quesIndex);
                } else {
                    return EmptyQuseBodyFragment.loadQueseFragment(typeIndex, quesIndex);
                }
            case "k"://阅读综合
                if (taskState == TaskState.LOOKING || taskState == TaskState.ORIGINAL) {
                    return CompoundMatchQuseBodyLookingFragment.loadQueseFragment(typeIndex, quesIndex, childIntex, defaultLoadChildIndex,defaultItemIndex);
                } else {
                    return CompoundMatchQuseBodyFragment.loadQueseFragment(typeIndex, quesIndex, childIntex, defaultLoadChildIndex,defaultItemIndex);
                }
        }
        return EmptyQuseBodyFragment.loadQueseFragment(typeIndex, quesIndex);
    }


}
